package renderer2;

import org.lwjgl.opengl.GL11;

import world.World;

public class WorldRenderer extends GenericRenderer<World> {
	public static final int radiusSize = 5;
	public static final int heightSize = 16;
	public static final int bufferSize = 3;
	public static final int renderDistance = 64;

	private ChunkRenderer[] chunkRenderers;
	private int renderChunksWide;
	private int renderChunksTall;
	private int renderChunksDeep;

	private int minBlockX;
	private int minBlockY;
	private int minBlockZ;
	private int maxBlockX;
	private int maxBlockY;
	private int maxBlockZ;

	private int glRenderListBase;
	private World world;

	public void render(World world) {

		this.world = world;
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glPushMatrix();
        GL11.glLoadIdentity();
        GL11.glColor3f(1.0f, 0.0f, 0.0f); /* red */
        GL11.glLineWidth(3);
        
		int j = GL11.glGenLists(1);
		Tessellator tessellator = Tessellator.instance;
		AABB  aabb  = new AABB (0.0f,0.0f,0.0f,10.0f,10.0f,10.0f);
		// this.glRenderListBase = generateDisplayLists((2 * radiusSize) * (2 * radiusSize) * (2 * heightSize) * 3);

		GL11.glNewList(j, GL11.GL_COMPILE);
		tessellator.setTranslation(0.0, 0.0,0.0);
		GL11.glTranslated(0, 0, 0);
		renderAABB(tessellator,aabb);
		tessellator.draw();
		GL11.glEndList();
		GL11.glPopMatrix();

	}
	
   public static void renderAABB(Tessellator tessellator , AABB par0AxisAlignedBB)
   {
       //back
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.minZ);//left t0p back
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.minZ);//right tb
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.minY, par0AxisAlignedBB.minZ);//r b b
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.minY, par0AxisAlignedBB.minZ);// l b b
       
       //front
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.minY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.minY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.maxZ);
       
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.minY, par0AxisAlignedBB.minZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.minY, par0AxisAlignedBB.minZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.minY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.minY, par0AxisAlignedBB.maxZ);
       
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.minZ);
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.minZ);
       
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.minY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.minZ);
       tessellator.addVertex(par0AxisAlignedBB.minX, par0AxisAlignedBB.minY, par0AxisAlignedBB.minZ);
       
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.minY, par0AxisAlignedBB.minZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.minZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.maxY, par0AxisAlignedBB.maxZ);
       tessellator.addVertex(par0AxisAlignedBB.maxX, par0AxisAlignedBB.minY, par0AxisAlignedBB.maxZ);
    }
   


	public void loadRenderers() {
		this.renderChunksWide = renderDistance / 16 + 1;
		this.renderChunksTall = 16;
		this.renderChunksDeep = renderDistance / 16 + 1;
		this.chunkRenderers = new ChunkRenderer[this.renderChunksWide * this.renderChunksTall * this.renderChunksDeep];

		this.minBlockX = 0;
		this.minBlockY = 0;
		this.minBlockZ = 0;
		this.maxBlockX = this.renderChunksWide;
		this.maxBlockY = this.renderChunksTall;
		this.maxBlockZ = this.renderChunksDeep;

		int j = 0;
		for (int l = 0; l < this.renderChunksWide; ++l) {
			for (int i1 = 0; i1 < this.renderChunksTall; ++i1) {
				for (int j1 = 0; j1 < this.renderChunksDeep; ++j1) {
					int index = (j1 * this.renderChunksTall + i1) * this.renderChunksWide + l;
					this.chunkRenderers[index] = new ChunkRenderer(this.world, l * 16, i1 * 16, j1 * 16, this.glRenderListBase + j);
					j += 3;
				}
			}
		}

	}
}
